package com.genghis.ptas.querycourse;

import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.course.entity.CourseInfo;
import com.genghis.ptas.course.service.CourseService;
import com.genghis.ptas.homepage.service.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("/query/")
public class QueryController {

    @Autowired
    private HomePageService homePageService;

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "queryHotCourse", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> queryHotCourse() {
        List<CourseInfo> hotList = homePageService.findCourseHotList();
        return hotList.subList(0, Math.min(8, hotList.size()));
    }

    @RequestMapping(value = "queryInterestedCourse", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> queryInterestedCourse(CourseInfo courseInfo) {
        List<CourseInfo> interestedList = homePageService.findCourseInterestedListByCourseType(courseInfo.getCourseType());
        return interestedList.subList(0, Math.min(8, interestedList.size()));
    }

    @RequestMapping(value = "querySystemCourse", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> querySystemCourse() {
        List<CourseInfo> systemList = homePageService.findCourseSystemList();
        return systemList.subList(0, Math.min(6, systemList.size()));
    }

    @RequestMapping(value = "queryStageCourse", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> queryStageCourse(HttpServletRequest request) {
        Integer page = Integer.parseInt(request.getParameter("page"));
        List<CourseInfo> interestedList = courseService.findCourseList();
        Integer begin = (page - 1) * DictConstant.PAGE;
        Integer end = page * DictConstant.PAGE;
        return interestedList.subList(begin, Math.min(end, interestedList.size()));
    }

    @RequestMapping(value = "queryRank", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> queryWeekBank(HttpServletRequest request) {
        String page = request.getParameter("page");
        return homePageService.queryRank(page);
    }

    @RequestMapping(value = "queryAllStudy", method = {RequestMethod.POST})
    @ResponseBody
    public List<CourseInfo> queryAllStudy() {
        return homePageService.findCourseSystemList();
    }

}
